var server="https://www.pansoft.com/appstore/port/updateCheck";//获取升级描述文件服务器地址
mui.plusReady(update);
var url2;
var deploymentKey = '';
function update(){
	if(mui.os.ios){
		deploymentKey = 'oa';
	}else{
		deploymentKey = 'oa.apk';
	}
	plus.runtime.getProperty( plus.runtime.appid, function ( wgtinfo ) {
	  mui.getJSON(server,{"deploymentKey":deploymentKey,"appVersion":wgtinfo.version},function (data) {		
		var temp = data.updateInfo;
		if(temp.isUpdate==true){
			url2=temp.downloadURL;
			if(temp.isMandatory==true && temp.isincrement == false){//整包强制升级
				plus.ui.confirm( temp.description?temp.description:"您有新的更新", function(i){
					if(mui.os.ios){
						plus.runtime.openURL("itms-services://?action=download-manifest&url="+temp.downloadURL );
					}else{
						plus.runtime.openURL(temp.downloadURL);
					}
			    }, temp.appVersion, ["立即更新"] );
			}else if(temp.isMandatory==true && temp.isincrement == false){//整包非强制升级
				plus.ui.confirm( temp.description?temp.description:"您有新的更新", function(event){
				   if (event.index==0 ) {
					    if(mui.os.ios){
							plus.runtime.openURL("itms-services://?action=download-manifest&url="+temp.downloadURL );
						}else{
							plus.runtime.openURL(temp.downloadURL);
						}
				   }
			    }, temp.appVersion, ["立即更新","取　　消"] );
			}else if(temp.isincrement == true){//免重安装 增量升级	
				if(mui.os.ios){
					plus.ui.confirm( temp.description?temp.description:"您有新的更新", function(i){
				      downWgt(temp.downloadURL);// 下载升级包
			        }, temp.appVersion, ["立即更新"] );  
				}else{
					plus.ui.confirm( temp.description?temp.description:"您有新的更新", function(i){
                       plus.runtime.openURL(temp.downloadURL);
			        }, temp.appVersion, ["立即更新"] );
				}
			}	
		}
	  });
	});
}
//下载应用
function downWgt(wgtUrl){
    panUtil.ajaxLoding.start("下载升级文件...","style2");
    //plus.nativeUI.showWaiting("下载wgt文件...");
    plus.downloader.createDownload( wgtUrl, {filename:"_doc/update/"}, function(d,status){
        if ( status == 200 ) { 
            ///console.log("下载wgt成功："+d.filename);
            installWgt(d.filename); // 安装wgt包
        } else {
            //console.log("下载wgt失败！");
            plus.nativeUI.alert("下载wgt失败！");
        }
        panUtil.ajaxLoding.stop();
        //plus.nativeUI.closeWaiting();
    }).start();
}

// 更新应用资源
function installWgt(path){
	panUtil.ajaxLoding.start("安装升级文件...","style2");
    //plus.nativeUI.showWaiting("安装wgt文件...");
    plus.runtime.install(path,{},function(){
    	panUtil.ajaxLoding.stop();
        //plus.nativeUI.closeWaiting();
        //console.log("安装wgt文件成功！");
        plus.nativeUI.alert("应用资源更新完成！",function(){
            plus.runtime.restart();
        });
    },function(e){
        //plus.nativeUI.closeWaiting();
        panUtil.ajaxLoding.stop();
        plus.runtime.openURL(url2);
    });
}

